package com.ruoyi.system.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.HangyeUtils;
import com.ruoyi.system.domain.SysHangye;
import com.ruoyi.system.mapper.SysHangyeMapper;
import com.ruoyi.system.service.ISysHangyeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 行业管理Service业务层处理
 *
 * @author shuyan
 * @date 2020-12-30
 */
@Service
public class SysHangyeServiceImpl extends ServiceImpl<SysHangyeMapper, SysHangye> implements ISysHangyeService {
    private String cacheKey = "sysHangyeAll";
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysHangyeMapper sysHangyeMapper;

    @Override
    public List<SysHangye> queryList(SysHangye sysHangye) {
        LambdaQueryWrapper<SysHangye> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sysHangye.getName())) {
            lqw.like(SysHangye::getName, sysHangye.getName());
        }
        if (StringUtils.isNotBlank(sysHangye.getCode())) {
            lqw.like(SysHangye::getCode, sysHangye.getCode());
        }
        if (sysHangye.getLevel() != null) {
            lqw.eq(SysHangye::getLevel, sysHangye.getLevel());
        }
        if (StringUtils.isNotBlank(sysHangye.getCate())) {
            lqw.eq(SysHangye::getCate, sysHangye.getCate());
        }
        return this.list(lqw);
    }

    @Override
    public List<SysHangye> listAllWithCache() {
        List<SysHangye> list = redisCache.getCacheList(cacheKey);
        if (list == null || list.size() == 0) {
            list = queryList(new SysHangye());
            redisCache.setCacheList(cacheKey, list);
        }
        return list;
    }

    @Override
    public boolean clearCache() {
        boolean f1 = redisCache.deleteObject("hangye");
        boolean f2 = redisCache.deleteObject(cacheKey);
        return f1 || f2;
    }

    @Override
    @Cacheable(cacheNames = "hangye", key = "T(String).valueOf('eachlevel:').concat('_').concat(#code)")
    public Map<Integer, SysHangye> getEachLevelByCode(String code) {
        Map<String, String> eachLevelCodeMap = HangyeUtils.getEachLevelCodeMap(code);
        Map<Integer, SysHangye> eachVo = new HashMap<>(3);
        for (int i = 1; i <= 3; i++) {
            String key = "code" + i;
            String curCode = MapUtil.getStr(eachLevelCodeMap, key);
            SysHangye hangye = null;
            if (StrUtil.isNotEmpty(curCode)) {
                hangye = getByCode(curCode);
            }
            eachVo.put(i, hangye);
        }
        return eachVo;
    }

    @Override
    public SysHangye getByCode(String code) {
        LambdaQueryWrapper<SysHangye> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysHangye::getCode, code);
        return this.getOne(lqw);
    }

    @Override
    public boolean syncCate() {
        List<SysHangye> list = this.list();
        List<SysHangye> saveList = list.stream().map(h -> {
            return new SysHangye().setId(h.getId()).setCate(HangyeUtils.getCate(h.getCode()));
        }).collect(Collectors.toList());
        clearCache();
        return updateBatchById(saveList);
    }
}
